


package ciicos.controller;

import ciicos.service.LineLineService;
import ciicos.service.LinePlaceService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ciicos.annotation.JnpfField;
import ciicos.base.ActionResult;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.base.UserInfo;
import ciicos.base.vo.DownloadVO;
import ciicos.config.ConfigValueUtil;
import ciicos.exception.DataException;
import ciicos.onlinedev.model.OnlineImport.ExcelImFieldModel;
import ciicos.onlinedev.model.OnlineImport.ExcelImportModel;
import ciicos.onlinedev.model.OnlineImport.VisualImportModel;
import ciicos.permission.entity.UserEntity;
import org.springframework.transaction.annotation.Transactional;
import ciicos.base.entity.ProvinceEntity;
import ciicos.model.linenfc.*;
import ciicos.model.linenfc.LineNfcPagination;
import ciicos.entity.*;
import ciicos.util.*;
import ciicos.base.util.*;
import ciicos.base.vo.ListVO;
import ciicos.util.context.SpringContext;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.Cleanup;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ciicos.entity.LineNfcEntity;
import ciicos.service.LineNfcService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import ciicos.util.GeneraterSwapUtil;

import java.util.stream.Collectors;
import java.util.*;

import cn.xuyanwu.spring.file.storage.FileInfo;


/**
 * NFC管理
 *
 * @版本： V3.2.0
 * @版权：
 * @作者： 企业研发中心
 * @日期： 2025-10-03
 */
@Slf4j
@RestController
@Api(tags = "NFC管理", value = "line")
@RequestMapping("/api/line/LineNfc")
public class LineNfcController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private LineNfcService lineNfcService;

    @Autowired
    private LineLineService lineLineService;

    @Autowired
    private LinePlaceService linePlaceService;

    @Autowired
    private DataDicUtil dataDicUtil;

    /**
     * 列表
     *
     * @param lineNfcPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody LineNfcPagination lineNfcPagination) throws IOException {
        //树形判断是否有参数，有参数不造树
        List<LineNfcEntity> list = lineNfcService.getList(lineNfcPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<LineNfcListVO> listVO = JsonUtil.getJsonToList(list, LineNfcListVO.class);
        for (LineNfcListVO lineNfcVO : listVO) {
            ActionResult info = info(lineNfcVO.getId());
            Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
            lineNfcVO.setCode_name(lineNfcVO.getCode());
            lineNfcVO.setName_name(lineNfcVO.getName());
            lineNfcVO.setType_name(generaterSwapUtil.getDicName(lineNfcVO.getType(), "743830653363028037"));
            lineNfcVO.setStatus_name(generaterSwapUtil.getDicName(lineNfcVO.getStatus(), "743830601328492613"));
            lineNfcVO.setCreatetime_name(lineNfcVO.getCreatetime());
            lineNfcVO.setUpdatetime_name(lineNfcVO.getUpdatetime());
            lineNfcVO.setRemark_name(lineNfcVO.getRemark());
            Map<String, Object> linecodeMap = new HashMap<>();
            lineNfcVO.setLinecode_name(generaterSwapUtil.getPopupSelectValue("743867011288793541", "F_Code", "F_Code", lineNfcVO.getLinecode(), linecodeMap, "[]", -1, dataMap));
            Map<String, Object> placecodeMap = new HashMap<>();
            lineNfcVO.setPlacecode_name(generaterSwapUtil.getPopupSelectValue("743867035271823813", "F_Code", "F_Code", lineNfcVO.getPlacecode(), placecodeMap, "[]", -1, dataMap));
            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(lineNfcPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    @ApiOperation("根据编码获取详情")
    @PostMapping("/detailByCode")
    public ActionResult detailByCode(@RequestBody LineNfcPagination pagination) {
        String code = pagination.getCode();
        LineNfcEntity entity = AutoCodeUtil.getOneByCode(lineNfcService, code, LineNfcEntity::getCode);
        if (entity == null) {
            return ActionResult.success(new LineNfcListVO());
        }
        LineNfcListVO listVO = JsonUtil.getJsonToBean(entity, LineNfcListVO.class);
        listVO.setCode_name(listVO.getCode());
        listVO.setName_name(listVO.getName());
        listVO.setType_name(generaterSwapUtil.getDicName(listVO.getType(), "743830653363028037"));
        listVO.setStatus_name(generaterSwapUtil.getDicName(listVO.getStatus(), "743830601328492613"));
        listVO.setCreatetime_name(listVO.getCreatetime());
        listVO.setUpdatetime_name(listVO.getUpdatetime());
        listVO.setRemark_name(listVO.getRemark());

        LineLineEntity lineLineEntity = AutoCodeUtil.getOneByCode(lineLineService, listVO.getLinecode(), LineLineEntity::getCode);
        if (lineLineEntity != null) {
            listVO.setLineId(lineLineEntity.getId());
            listVO.setLinecode_name(lineLineEntity.getName());
        } else {
            listVO.setLinecode_name(listVO.getLinecode());
        }
        LinePlaceEntity linePlaceEntity = AutoCodeUtil.getOneByCode(linePlaceService, listVO.getPlacecode(), LinePlaceEntity::getCode);
        if (linePlaceEntity != null) {
            listVO.setPlaceId(linePlaceEntity.getId());
            listVO.setPlacecode_name(linePlaceEntity.getName());
        } else {
            listVO.setPlacecode_name(listVO.getPlacecode());
        }

        return ActionResult.success(listVO);
    }

    /**
     * 创建
     *
     * @param lineNfcForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid LineNfcForm lineNfcForm) throws DataException {
        ActionResult actionResult = lineNfcService.checkForm(lineNfcForm, lineNfcForm.getId());
        if (actionResult.getCode()!=200) {
            return actionResult;
        }
        String mainId = RandomUtil.uuId();

//        lineNfcForm.setCode(generaterSwapUtil.getBillNumber("nfccode", false));
        lineNfcForm.setCreatetime(DateUtil.getNow());
        LineNfcEntity entity = JsonUtil.getJsonToBean(lineNfcForm, LineNfcEntity.class);
        entity.setId(mainId);
        lineNfcService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<LineNfcInfoVO> info(@PathVariable("id") String id) {
        LineNfcEntity entity = lineNfcService.getInfo(id);
        LineNfcInfoVO vo = JsonUtil.getJsonToBean(entity, LineNfcInfoVO.class);
        if (vo.getCreatetime() != null) {
            vo.setCreatetime(vo.getCreatetime());
        }
        if (vo.getUpdatetime() != null) {
            vo.setUpdatetime(vo.getUpdatetime());
        }

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<LineNfcInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        LineNfcEntity entity = lineNfcService.getInfo(id);
        LineNfcInfoVO vo = JsonUtil.getJsonToBean(entity, LineNfcInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setCode(vo.getCode());

        vo.setType(generaterSwapUtil.getDicName(vo.getType(), "743830653363028037"));

        vo.setStatus(generaterSwapUtil.getDicName(vo.getStatus(), "743830601328492613"));

        vo.setCreatetime(vo.getCreatetime());

        vo.setUpdatetime(vo.getUpdatetime());

        vo.setRemark(vo.getRemark());

        Map<String, Object> linecodeMap = new HashMap<>();
        vo.setLinecode(generaterSwapUtil.getPopupSelectValue("743867011288793541", "F_Code", "F_Code", vo.getLinecode(), linecodeMap, "[]", -1, dataMap));

        Map<String, Object> placecodeMap = new HashMap<>();
        vo.setPlacecode(generaterSwapUtil.getPopupSelectValue("743867035271823813", "F_Code", "F_Code", vo.getPlacecode(), placecodeMap, "[]", -1, dataMap));


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param lineNfcForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid LineNfcForm lineNfcForm) throws DataException {
        ActionResult actionResult = lineNfcService.checkForm(lineNfcForm, id);
        if (actionResult.getCode()!=200) {
            return actionResult;
        }
        LineNfcEntity entity = lineNfcService.getInfo(id);
        if (entity != null) {
            lineNfcForm.setUpdatetime(DateUtil.getNow());
            LineNfcEntity subentity = JsonUtil.getJsonToBean(lineNfcForm, LineNfcEntity.class);
            //code不能更改
//            subentity.setCode(entity.getCode());
            subentity.setCreatetime(entity.getCreatetime());
            boolean b1 = lineNfcService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        LineNfcEntity entity = lineNfcService.getInfo(id);
        if (entity != null) {
            lineNfcService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }


}
